package com.bank.chat.controller;

import com.alibaba.fastjson.JSONObject;
import com.bank.chat.service.ChatRecordService;
import com.bank.chat.service.ChatService;
import com.bank.chat.websocket.WebSocketServer;
import com.bank.common.commonUtil.AuthContextHolderUtil;
import com.bank.model.vo.chat.ChatRecordVo;
import com.bank.model.vo.chat.UserListVo;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author callumEm
 * @version 1.0
 * @create 2023/4/16 10:47
 */
@RestController
@RequestMapping(value = "/chat")
public class ChatController {
    @Autowired
    ChatService chatService;
    @Autowired
    ChatRecordService chatRecordService;
    @Autowired
    WebSocketServer webSocketServer;

//    @GetMapping("/come")
//    public String toChatroom() {
//        try {
//            webSocketServer.run();
//        } finally {
//            webSocketServer.close();
//        }
//        return "chatroom";
//    }
    @Bean
    public void initChat() {
            new Thread(webSocketServer).start();
    }

    @PostMapping("/register")
    public void register(JSONObject param, ChannelHandlerContext ctx) {
        chatService.register(param, ctx);
    }

    @GetMapping("/chatrecord")
    public List<ChatRecordVo> queryChatRecord(@RequestParam Long toId, HttpServletRequest request) {
        return chatRecordService.queryChatRecords(AuthContextHolderUtil.getUserId(request), toId);
    }

    //    @PostMapping("/send")
//    public void addChatRecord(@RequestBody ){
//        return chatRecordService.queryChatRecords(AuthContextHolderUtil.getUserId(request),toId);
//    }
    @GetMapping("/list/friend")
    public List<UserListVo> getUserListVos() {
        return chatService.getUserListVos();
    }
}
